package com.homwee.tvap.common.comSettingInterface;

import android.content.ActivityNotFoundException;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

//import com.changhong.tvos.common.MiscManager;
//import com.changhong.tvos.common.PictureManager;
//import com.changhong.tvos.common.TVManager;
//import com.changhong.tvos.common.exception.IllegalValueException;
//import com.changhong.tvos.service.TvMiscManager;
import com.mediatek.twoworlds.tv.MtkTvConfig;
import com.mediatek.twoworlds.tv.common.MtkTvConfigTypeBase;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

//page 8
public class factoryPage8{
	private static final String TAG = "CHFactory.factoryPage8";
	//	private static PictureManager pictureManager;
//	private TvMiscManager mTvMiscManager = null;
	static factoryPage8 Interface =null;
	MenuConfigManager mCfg =MenuConfigManager.getInstance(null);
	MtkTvConfig mMtkTvConfig=null;

	private factoryPage8(Context context){
//		TVManager tvManager = TVManager.getInstance(context);
		try {
//			pictureManager = tvManager.getPictureManager();
			//mTvMiscManager =TvMiscManager.getInstance();
			mMtkTvConfig=MtkTvConfig.getInstance();
		} catch (Exception e) {
			// TODO: handle exception
			Log.v("tv","pictureManager ==null");
		}
	} 
	
	public static factoryPage8 getInstance(Context context){
		if(Interface ==null){
			Interface = new factoryPage8(context);
		}
		return Interface;
	}
	
	public int getBackLight() throws Exception{
		int backLight =100;
		if(mMtkTvConfig!=null){
			backLight =mMtkTvConfig.getConfigValue(MtkTvConfigTypeBase.CFG_DISP_DISP_DISP_BACK_LIGHT);
		}
		return backLight;
	}
	
	public void setBackLight(int value) throws Exception{
		if(mMtkTvConfig !=null)
			mMtkTvConfig.setConfigValue(MtkTvConfigTypeBase.CFG_DISP_DISP_DISP_BACK_LIGHT, value);
	}

	public void playDemoVideo(String path,Context context){
		Intent intent = new Intent();
		ComponentName comp = new ComponentName("com.homwee.mmp.fileexplorer",
				"com.changhong.mmp.videoplayer.ChangHongVideoPlayerActivity");
		intent.setComponent(comp);
		HashMap<String, Object> movieNode = new HashMap<String, Object>();
		movieNode.put("title", "demoVideo.mp4"); //filename
		movieNode.put("cn", 1);
		String[] str = new String[]{path}; //path
		movieNode.put("playUrls", str);
		ArrayList<Map<String, Object>> videoPlayNodeList = new ArrayList<Map<String, Object>>();
		videoPlayNodeList.add(movieNode);
		Bundle mData = new Bundle();
		mData.putInt("playSourceType", 0);
		mData.putInt("curPos", 0);
		intent.putExtra("movieNode", videoPlayNodeList);
		intent.putExtras(mData);
		intent.putExtra("show_mode_play", true);
		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		context.startActivity(intent);
	}

	public void copyVideoToData(String oldPath, String newPath) {
		try {
			File mOldFile=new File(oldPath);
			if (mOldFile.isFile()) {
				FileInputStream input = new FileInputStream(mOldFile);
				FileOutputStream output = new FileOutputStream(newPath + "/" +
						(mOldFile.getName()).toString());
				Log.d(TAG, "copyVideoToData: " + mOldFile.getName() + "copy success");
				byte[] b = new byte[1024 * 5];
				int len;
				while ((len = input.read(b)) != -1) {
					output.write(b, 0, len);
				}
				output.flush();
				output.close();
				input.close();

			}
		} catch (Exception e) {
			System.out.println("error in copy file");
			e.printStackTrace();
		}
	}
	
}